﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace TroTro
{
    class Score
    {
        #region Fields

        private float _currentScore;
        private float _addScore;
        private float _subScore;
        private bool _addingScore;
        private bool _subingScore;
        private char _preAddScore;
        private char _preSubScore;
        private SpriteBatch _spriteBatch;
        private SpriteFont _font;
        private Game _game;

        //Timing variables for showing add score
        private int _addTime;
        private const int AddDuration = 1000;

        public float CurrentScore
        {
            get { return _currentScore; }
        }
        public Score(Game game)
        {
            _game = game;
            _currentScore = 0.0F;
            _addScore = 0.0F;
            _addingScore = false;
            _preAddScore = '+';
            LoadContent();
        }

        #endregion

        #region XNA Methods

        public void LoadContent()
        {
            _font = _game.Content.Load<SpriteFont>(@"sprites\scoreFont");
        }

        public void Update()
        {
            _currentScore += 0.25F;

            if (_addingScore && Environment.TickCount > (_addTime + AddDuration) || _subingScore && Environment.TickCount > (_addTime + AddDuration))
            {
                _addingScore = false;
                _subingScore = false;
            }
        }

        #endregion

        #region Other Methods

        public void AddScore(float score)
        {
            _addTime = Environment.TickCount;
            _addingScore = true;
            _preAddScore = score >= 0 ? '+' : '-';
            _addScore = Math.Abs(score);
            _currentScore += score;
        }

        private String BuildString()
        {
            var currentScore = Convert.ToString(Convert.ToInt32(_currentScore));
            var addScore = Convert.ToString(Convert.ToInt32(_addScore));

            return _addingScore ? String.Concat("Score:", currentScore, "" + _preAddScore, addScore) : String.Concat("Score:", currentScore);
        }

        public void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            spriteBatch.DrawString(_font, BuildString(), new Vector2(10, 10), Color.White);
        }

        #endregion

    }
}
